package ca.etsmtl.log430.lab2;

/**
 * This class reads from the InputFile and instantiates the Course objects in
 * the system. It is assumed that the InputFile is in the local directory,
 * contains the active students, and each line of input is read and expected in
 * the following format: a field oriented and space-separated text file that
 * lists all of the students. The fields are as follows:
 * <pre>
 *     ABCD70123101 Franklin John LOG LOG120 MAT115 CHM101
 *     |            |        |    |   |
 *     |            |        |    |   Courses taken by this student
 *     |            |        |    Program Affiliation
 *     |            |        Student's First Name
 *     |            Student's Last Name 
 *     Student ID</pre>
 * 
 * The etudiantsLOG.txt file has been provided as an example.
 *
 * @author A.J. Lattanze, CMU 
 * @version 1.3, 2011-Feb-02
 */

/*
 * Modification Log
 * *********************************************************************** v1.3,
 * 2011-Feb-02, R. Champagne - Various refactorings, javadoc comments.
 * 
 * v1.2, 2002-May-21, R. Champagne - Adapted for use at ETS.
 * 
 * v1.1, 2001-Jan-25, G.A. Lewis - Bug in ParseStudentText. There was a bug in
 * that was causing it not to read the last course into the list of courses
 * taken.
 * 
 * v1.0, 12/29/99, A.J. Lattanze - Original version.
 * ***********************************************************************
 */

public class StudentReader extends LineOfTextFileReader {

	/**
	 * The list of students.
	 */
	private StudentList listOfStudents = new StudentList();

	public StudentReader() {

		listOfStudents = null;

	} // Constructor #1

	public StudentReader(String InputFile) {

		listOfStudents = readStudentListFromFile(InputFile);

	} // Constructor #2

	/**
	 * Reads a line of text. The line of text is passed to the ParseStudentText
	 * method where it is parsed and a Student object is returned. The Student
	 * object is then added to the list. When a null is read from the Student
	 * file the method terminates and returns the list to the caller. A list
	 * that points to null is an empty list.
	 * 
	 * @param InputFile
	 * @return The list of students
	 */
	public StudentList readStudentListFromFile(String InputFile) {

		String StudentText; // Line of text from the Students.txt file
		boolean Done; // End of the file - stop processing

		// New student list object - this will contain all of the students in
		// the
		// Students.txt file

		StudentList ListObject = new StudentList();

		if (openFile(InputFile)) {

			Done = false;

			while (!Done) {

				try {

					StudentText = readLineOfText();

					if (StudentText == null) {

						Done = true;

					} else {

						ListObject.addStudent(parseStudentText(StudentText));

					} // if

				} // try

				catch (Exception Error) {

					return (null);

				} // catch

			} // while

		} else {

			return (null);

		} // if

		return (ListObject);

	} // GetStudentList

	public StudentList getListOfStudents() {
		return listOfStudents;
	}

	public void setListOfStudents(StudentList listOfStudents) {
		this.listOfStudents = listOfStudents;
	}

	/**
	 * Parse lines of text that are read from the text file containing student
	 * information in the above format.
	 * 
	 * @param LineOfText
	 * @return
	 */
	private Student parseStudentText(String LineOfText) {

		boolean Done = false; // Indicates the end of parsing
		String Token; // String token parsed from the line of text
		int TokenCount = 0; // Number of tokens parsed
		int FrontIndex = 0; // Front index or character position
		int BackIndex = 0; // Rear index or character position

		// Create a student object to record all of the info parsed from
		// the line of text

		Student NewStudent = new Student();

		while (!Done) {

			BackIndex = LineOfText.indexOf(' ', FrontIndex);

			if (BackIndex == -1) {

				Done = true;
				Token = LineOfText.substring(FrontIndex);

			} else {

				Token = LineOfText.substring(FrontIndex, BackIndex);
			}

			switch (TokenCount) {

			case 0: // Student Social Security Number
				NewStudent.setStudentID(Token);
				FrontIndex = BackIndex + 1;
				TokenCount++;
				break;

			case 1: // Student's last name
				NewStudent.setLastName(Token);
				FrontIndex = BackIndex + 1;
				TokenCount++;
				break;

			case 2: // Student's First name
				NewStudent.setFirstName(Token);
				FrontIndex = BackIndex + 1;
				TokenCount++;
				break;

			case 3: // Program affiliation (e.g. LOG, ELE, ...)
				NewStudent.setProgram(Token);
				FrontIndex = BackIndex + 1;
				TokenCount++;
				break;

			default:
				// This is where the courses are added to the student'
				// courses-taken list within the student object
				// Note that there are no details other than the course
				// ID that is recorded in the student's courses-taken
				// list.
				NewStudent.getCoursesTakenList().addCourse(new Course(Token));
				FrontIndex = BackIndex + 1;
				break;

			} // end switch

		} // end while

		return (NewStudent);

	} // ParseStudentText

} // StudentReader
